package com.todo.strategy.offline.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.MathUtils;
import com.todo.strategy.offline.infrastructure.convert.MemoConvert;
import com.todo.strategy.offline.infrastructure.entity.condition.MemoTemplateCondition;
import com.todo.strategy.offline.infrastructure.entity.domain.MemoTemplateContentDO;
import com.todo.strategy.offline.infrastructure.entity.po.MemoTemplatePO;
import com.todo.strategy.offline.infrastructure.mapper.MemoTemplateMapper;
import com.todo.strategy.offline.infrastructure.repository.MemoTemplateRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author azhebuxing
 * @date 2024/11/28 02:11
 * @description
 */
@Slf4j
@Repository("memoTemplateRepository")
public class MemoTemplateRepositoryImpl implements MemoTemplateRepository {

    @Resource
    private MemoTemplateMapper memoTemplateMapper;

    @Override
    public Boolean insertMemoTemplate(MemoTemplateContentDO memoTemplateContentDO) {
        if (memoTemplateContentDO == null) {
            return false;
        }
        if (!baseFieldCheck(memoTemplateContentDO)) {
            return false;
        }
        MemoTemplatePO memoTemplatePO = MemoConvert.convertToMemoTemplatePO(memoTemplateContentDO);
        return memoTemplateMapper.insertMemoTemplatePO(memoTemplatePO) > 0;
    }

    @Override
    public Boolean updateMemoTemplate(MemoTemplateContentDO memoTemplateContentDO) {
        if (memoTemplateContentDO == null || memoTemplateContentDO.getId() == null) {
            return false;
        }
        //更新操作是根据是否有值确定的，所以无需做数据校验了
        MemoTemplatePO memoTemplatePO = MemoConvert.convertToMemoTemplatePO(memoTemplateContentDO);
        return memoTemplateMapper.updateMemoTemplatePOById(memoTemplatePO) > 0;
    }

    @Override
    public MemoTemplateContentDO queryMemoTemplateContentDOById(Long templateId) {
        if (templateId == null) {
            return null;
        }
        MemoTemplatePO memoTemplatePO = memoTemplateMapper.queryMemoTemplatePOById(templateId);
        return MemoConvert.convertToMemoTemplateContentDO(memoTemplatePO);
    }

    @Override
    public Boolean removeMemoTemplateContentDOById(Long id) {
        if (id == null || id < 0) {
            return false;
        }
        return memoTemplateMapper.removeMemoTemplatePOById(id) > 0;
    }

    @Override
    public PageResult<MemoTemplateContentDO> queryMemoTemplatePOByCondition(MemoTemplateCondition memoTemplateCondition) {
        PageResult<MemoTemplateContentDO> pageResult = new PageResult<>();
        if (memoTemplateCondition == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        pageResult.setPageNum(memoTemplateCondition.getPageIndex());
        pageResult.setPageSize(memoTemplateCondition.getPageSize());
        long count = queryCountByCondition(memoTemplateCondition);
        if (count <= 0) {
            return pageResult;
        }
        pageResult.setPageCount((int) MathUtils.divide(count, memoTemplateCondition.getPageSize()));
        pageResult.setTotal(count);
        List<MemoTemplatePO> memoTemplatePOList = memoTemplateMapper.queryMemoTemplatePOByCondition(memoTemplateCondition);
        if (CollectionUtils.isEmpty(memoTemplatePOList)) {
            pageResult.setHasNext(false);
            return pageResult;
        }
        pageResult.setResults(MemoConvert.convertToMemoTemplatePOList(memoTemplatePOList));
        pageResult.setCurrentIndex(memoTemplatePOList.get(0).getId());
        pageResult.setHasNext(memoTemplatePOList.size() >= memoTemplateCondition.getPageSize());
        return pageResult;
    }

    @Override
    public Boolean updateMemoStatusById(Long id, int oldStatus, int newStatus) {
        if (id == null || id < 0) {
            return false;
        }
        return memoTemplateMapper.updateMemoStatusById(id, oldStatus, newStatus);
    }

    private long queryCountByCondition(MemoTemplateCondition memoTemplateCondition) {
        if (memoTemplateCondition == null || memoTemplateCondition.getPageSize() <= 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return memoTemplateMapper.queryCountByCondition(memoTemplateCondition);
    }

    private boolean baseFieldCheck(MemoTemplateContentDO memoTemplateContentDO) {
        if (memoTemplateContentDO == null) {
            return false;
        }
        if (memoTemplateContentDO.getUserId() == null || memoTemplateContentDO.getName() == null || memoTemplateContentDO.getContentModel() == null) {
            log.warn("MemoTemplateRepository#baseFieldCheck 核心字段校验失败，默认操作失败 memoTemplateContentDO：【{}】", JSONObject.toJSONString(memoTemplateContentDO));
            return false;
        }
        if (memoTemplateContentDO.getBaseType() == null || memoTemplateContentDO.getSuggestStrategy() == null || memoTemplateContentDO.getIsPlatform() == null) {
            log.warn("MemoTemplateRepository#baseFieldCheck 核心字段校验失败，默认操作失败 memoTemplateContentDO：【{}】", JSONObject.toJSONString(memoTemplateContentDO));
            return false;
        }
        return true;
    }
}
